package com.achobeta.recruitment.utils;

import com.achobeta.recruitment.config.AppConfig;
import com.achobeta.recruitment.model.Manager;
import com.achobeta.recruitment.model.WxUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-09-02
 * Time: 22:39
 */

public class SessionUtils {
    /**
     * 得到当前登录的管理员
     * @param request 请求
     * @return 当前登录用户信息
     */
    public static Manager getManager(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        return session == null ? null : (Manager) session.getAttribute(AppConfig.Manager_SESSION_KEY);
    }

    /**
     * 保存管理员信息
     * @param request 请求
     */
    public static void setManager(HttpServletRequest request, Manager managerInfo) {
        HttpSession session = request.getSession(true);
        session.setAttribute(AppConfig.Manager_SESSION_KEY, managerInfo);
        session.setMaxInactiveInterval(-1);//session永不过期
    }

    /**
     * 注销
     * @param request 请求
     */
    public static void remove(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute(AppConfig.Manager_SESSION_KEY) != null) {
            session.removeAttribute(AppConfig.Manager_SESSION_KEY);
//            session.invalidate();消除 session
        }
    }

    /**
     * 得到当前登录的管理员
     * @param request 请求
     * @return 当前登录用户信息
     */
    public static WxUser getWxUser(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        return session == null ? null : (WxUser) session.getAttribute(AppConfig.WX_SESSION_KEY);
    }

    /**
     * 保存管理员信息
     * @param request 请求
     */
    public static void setWxUser(HttpServletRequest request, WxUser wxUserInfo) {
        HttpSession session = request.getSession(true);
        session.setAttribute(AppConfig.WX_SESSION_KEY, wxUserInfo);
        session.setMaxInactiveInterval(-1);//session永不过期
    }
}
